package com.biodiv.datamapper.factory;

import com.biodiv.miracle.ioc.context.ApplicationContext;
import com.biodiv.miracle.ioc.factory.FactoryBean;
import com.biodiv.miracle.ioc.factory.FactoryBeanRegister;
import com.biodiv.miracle.support.utils.MiracleUtils;
import com.biodiv.datamapper.support.Mapper;
import com.biodiv.datamapper.support.MapperScan;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * @author: tianquan
 * @date: 2025-05-30  09:05
 * @description: 扫描注册Mapper接口
 */
public class MapperRegisterFactory extends FactoryBeanRegister {

    @Override
    public Collection<FactoryBean<?>> scanFactoryBean(Class<?> mianClass, ApplicationContext applicationContext) {
        MapperScan mapperScan = mianClass.getAnnotation(MapperScan.class);
        if (mapperScan == null) {
            return Collections.emptyList();
        }
        String scanPackage = mapperScan.value();
        List<Class<?>> classList = parsePackage(scanPackage);
        List<FactoryBean<?>> factoryBeans = new ArrayList<>();
        for (Class<?> aClass : classList) {
            Mapper annotation = aClass.getAnnotation(Mapper.class);
            // 跳过非Mapper接口
            if (annotation == null && !aClass.isInterface()){
                continue;
            }
            MapperFactoryBean<?> factoryBean = new MapperFactoryBean<>(aClass);
            factoryBeans.add(factoryBean);
        }
        return factoryBeans;
    }

    private List<Class<?>> parsePackage(String scanPackage) {
        return loadClass(scanPackage);
    }




}
